// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var js_supervisionNotice = require("./supervisionNotice.js");
	var css_tallyDetail = require("./tallyDetail.css");
	var leftMenu = require("../leftMenu/leftMenu");
	var js_platform_main = require("./platform_main.js");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var recieveNoticeId;
	var tallyingId;
	var inventoryId;
	var type;
	var left;
	var init = function(page) {

		recieveNoticeId = page.params.recieveNoticeId || base.getUrlParam("recieveNoticeId");
		tallyingId = page.params.tallyingId || base.getUrlParam("tallyingId");
		inventoryId = page.params.inventoryId || base.getUrlParam("inventoryId");
		left = page.params.left || base.getUrlParam("left");

		type = page.params.type || base.getUrlParam("type");
		inquireBulkstockInventoryDetail(inventoryId);
		initEvent();
		router.init();
		$(".left").removeClass("none");
		leftMenu.init(6);
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		js_platform_main.init(0);
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		//收货确认和监管确认
		$(".supervisionNotice_list").unbind("click").click(function() {
			router.route("main/supervisionNotice_list.html?left=3", true);
		});

		//理货任务详情
		$(".supervisionNotice").unbind("click").click(function() {
			router.route("main/supervisionNotice.html?tallyingId=" + tallyingId + "&left=3", true);
		});

	};

	//查询融资箱货明细
	var inquireBulkstockInventoryDetail = function(inventoryId) {
		base.fnAjax({
			action: "inquireBulkstockInventoryDetail.json",
			oData: {
				inventoryId: inventoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#tallyDetailTpl").html();
					var html = juicer(tpl, data);
					$(".tallyDetail_main_content").html(html);
					$.each($("#tallyDetail input[name=poundsOfSign]").val().split(','), function(i, val) {
						var _this = this;
						if (_this != "") {
							var str = '<a style="margin-left:15px;" target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'">'+tools.inquireFile(_this)+'<a>';
							$("#tallyDetail input[name=poundsOfSign]").parent().append(str);
						}
					});
					
					$.each($(".bulkstockFilesUuids"), function(i, val) {
						showImg($(this).parent().find(".uploadImgP"), val.value);
					});
					$.each($(".suffixTallyFiles"), function(i, val) {
						showImg($(this).parent(), val.value);
					});
					$.each($(".preTallyFiles"), function(i, val) {
						showImg($(this).parent(), val.value);
					});
					$.each($("#soundRecorde1"), function(i, val) {
						showImg($(this).parent(), val.value);
					});
					$.each($("#abnormalFile1"), function(i, val) {
						showImg($(this).parent(), val.value);
					});
					loadFinanceWaybillList(data.datas[0].financeId);
					tools.showLogisticsTrajectory(data.datas[0].caseNumber, data.datas[0].waybillNumber, $("#tallyDetail .logisticsDetailInfo"));
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	var loadFinanceWaybillList = function(financeId) {

			base.fnAjax({
				action: "inquireFinanceWaybillList.json",
				oData: {
					financeId: financeId
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						var tpl = $("#financeWaybillListTpl").html();
						var html = juicer(tpl, data);
						$(".waybillNumber").html(html);

						if ("readOnly" == type) {
							databind.disableInput($("#submitContainerTallying"));
							$(".uploadImgP").find(".file").css("display", "none");
							$(".closeLayer").css("display", "none");
							$(".uploadFilepreTallyP").css("display", "none");
							$(".uploadFilesuffixTallyP").css("display", "none");
							$(".soundRecordeP").css("display", "none");
							$(".abnormalFileP").css("display", "none");
							$("#submitTally").css("display", "none");
							//			html = '<div id="updateTally" class="updateTally dialogBtn dialogHandlerBtn">修改理货</div>';
							//			$("#submitTally").parent().append(html);

							$("#updateTally").unbind("click").click(function() {
								$(".uploadImgP").find(".file").css("display", "block");
								$(".closeLayer").css("display", "block");
								$(".uploadFilepreTallyP").css("display", "block");
								$(".uploadFilesuffixTallyP").css("display", "block");
								$("#submitTally").css("display", "block");
								$("#submitTally").css("display", "block");
								databind.enableInput($("#submitContainerTallying"));
								$("#updateTally").css("display", "none");

							});
						}
						
						$("#tallyDetail .waybillNumber").val($("#tallyDetail .waybillNumber").attr("id"));
					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//显示图片
	var showImg = function(dom, fileUuid) {
		var fileUuids = fileUuid.split(",");
		var html = "";
		var imgSrc = "";
		for (var i = 0; i < fileUuids.length; i++) {
			imgSrc = fileUuids[i];
			if (imgSrc.length != 0) {
				html = "<label class=\"finance-file\"><a href= \"/finance/api/1.0/downloadFile.file?fileUuid=" + imgSrc + "\" target=\"_blank\"><img  src=\"/finance/api/1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  /> <a class=\"closeLayer\" ></a></label>";
				$(dom).append(html);
			}

		}
	}

	var uploadFile = function(domId, id) { //指定p标签的dom的ID,img的id
		if ($("#" + id).val() != "") {
			var fileType = "jpg,png,bmp";
			var prompt = validate.validateFileType($("#" + domId).find("#" + id), fileType);
			if (prompt.length > 1) {
				base.alert(2, prompt);
				return;
			}
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: id,
				dataType: 'json',
				success: function(data) {
					imgSrc = data.data[0].fileUuid;
					var html = "<label class=\"finance-file\"><img  src=\"/finance/api/1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  /> <a class=\"closeLayer\" ></a></label>";

					$("#" + domId).append(html);
					savefilesUuid($("#" + domId));
					if ($("#" + domId).find(".finance-field-img").length == 8) {
						$("#" + domId).find(".file").css("display", "none");
					}
				},
				complete: function(xml) {},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}

	}

	//更新fileUuuid保存字段
	var savefilesUuid = function(dom) {
		var filesUuids = "";
		var imgDom = dom.find(".finance-field-img");
		var number = imgDom.length;
		$.each(imgDom, function(i, object) {
			if (i == number - 1) {
				filesUuids += object.alt;
			} else {
				filesUuids += object.alt + ",";
			}

		});
		dom.parent().find(".bulkstockFilesUuids").val(filesUuids);
		dom.parent().find(".suffixTallyFiles").val(filesUuids);
		dom.parent().find(".preTallyFiles").val(filesUuids);
	}
	return {
		init: init
	};
});